package service

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/gogf/gf/v2/database/gdb"
)

type ImlOrmBusiness interface {
	QueryOne(sql string) (int, map[string]interface{})
	QueryAll(sql string) (int, []map[string]interface{})
	Update(table string, data interface{}, condition interface{}, args ...interface{}) int
	Exists(sql string) int
	Exec(sql string) int
	Delete(table string, data interface{}, condition interface{}, args ...interface{}) int
	Insert(table string, data interface{}) int
	Replace(table string, data interface{}) int
}

type SOrmBusiness struct {
}

func NewImlOrmBusiness() *SOrmBusiness {
	MysqlConfig := gdb.Config{}
	file, err := os.Open("./config/mysql.json")
	if err != nil {
		fmt.Printf("Open mysql.json Failed [Error:%s]", err.Error())
		return nil
	}
	decoder := json.NewDecoder(file)

	err = decoder.Decode(&MysqlConfig)
	if err != nil {
		fmt.Println("Error:", err)
	}
	gdb.SetConfig(MysqlConfig)
	return &SOrmBusiness{}
}
func (p *SOrmBusiness) QueryOne(ctx context.Context, sql string) (int, map[string]interface{}) {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("QueryOne err:%s\n", err.Error())
		return -1, nil
	}
	res, err2 := db.GetOne(ctx, sql)
	if err2 != nil {
		fmt.Printf("QueryOne err:%s\n", err2.Error())
	} else {
		if res.IsEmpty() == false {
			vmap := res.Map()
			return 1, vmap
		} else {
			return -1, nil
		}

	}
	return -1, nil
}
func (p *SOrmBusiness) QueryAll(ctx context.Context, sql string) (int, []map[string]interface{}) {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("QueryAll err:%s\n", err.Error())
		return -1, nil
	}
	res, err := db.GetAll(ctx, sql)
	if err == nil {
		if res.IsEmpty() == false {
			vmap := res.List()
			return 1, vmap
		} else {
			return -1, nil
		}

	} else {
		fmt.Printf("QueryAll err:%s\n", err.Error())
	}
	return -1, nil
}
func (p *SOrmBusiness) Update(ctx context.Context, table string, data interface{}, condition interface{}, args ...interface{}) int {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("Update err:%s\n", err.Error())
		return -1
	}
	res, err := db.Update(ctx, table, data, condition, args...)
	if err == nil {
		num, err := res.RowsAffected()
		if err == nil && num >= 0 {
			return 1
		}
	} else {
		fmt.Printf("QueryAll err:%s\n", err.Error())
	}
	return -1
}
func (p *SOrmBusiness) Exists(ctx context.Context, sql string) int {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("Exists err:%s\n", err.Error())
		return -1
	}
	res, err2 := db.GetOne(ctx, sql)
	if res.IsEmpty() == false {
		return 1
	} else if err2 != nil {
		fmt.Printf("Exists err:%s\n", err2.Error())
	}
	return -1
}
func (p *SOrmBusiness) Exec(ctx context.Context, sql string) int {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("Exec err:%s\n", err.Error())
		return -1
	}
	res, err := db.Exec(ctx, sql)
	if err == nil {
		num, err := res.RowsAffected()
		if err == nil && num >= 0 {
			return 1
		}
	} else {
		fmt.Printf("Exec err:%s\n", err.Error())
	}
	return -1
}
func (p *SOrmBusiness) Delete(ctx context.Context, table string, condition interface{}, args ...interface{}) int {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("Delete err:%s\n", err.Error())
		return -1
	}
	res, err := db.Delete(ctx, table, condition, args...)
	if err == nil {
		num, err := res.RowsAffected()
		if err == nil && num >= 0 {
			return 1
		}
	} else {
		fmt.Printf("Delete err:%s\n", err.Error())
	}
	return -1
}
func (p *SOrmBusiness) Insert(ctx context.Context, table string, data interface{}) int {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("Insert err:%s\n", err.Error())
		return -1
	}
	res, err := db.Insert(ctx, table, data)
	if err == nil {
		num, err := res.RowsAffected()
		if err == nil && num >= 0 {
			return 1
		}
	} else {
		fmt.Printf("Insert err:%s\n", err.Error())
	}
	return -1
}
func (p *SOrmBusiness) Replace(ctx context.Context, table string, data interface{}) int {
	db, err := gdb.Instance("database")
	if err != nil {
		fmt.Printf("Replace err:%s\n", err.Error())
		return -1
	}
	res, err := db.Replace(ctx, table, data)
	if err == nil {
		num, err := res.RowsAffected()
		if err == nil && num >= 0 {
			return 1
		}
	} else {
		fmt.Printf("Replace err:%s\n", err.Error())
	}
	return -1
}
